<?php
namespace other\email;
use other\Log;
use other\ShopingConn;
use other\Table;
use think\Db;

/*
 DROP TABLE IF EXISTS `orders_email_time`;
CREATE TABLE `orders_email_time` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `order_id` int(11) NOT NULL COMMENT '订单ID',
  `created_at` int(11) DEFAULT NULL COMMENT '创建时间',
  `pay_time` int(11) DEFAULT NULL COMMENT '支付时间',
  `dm` int(11) DEFAULT NULL COMMENT '所属分',
  `dscale` int(11) DEFAULT NULL COMMENT '所属刻度',
  `dscale_rule_one` int(11) DEFAULT NULL COMMENT '规则一',
  `dscale_rule_two` int(11) DEFAULT NULL COMMENT '规则一',
  `dscale_rule_three` int(11) DEFAULT NULL COMMENT '规则一',
  `dscale_rule_four` int(11) DEFAULT NULL COMMENT '规则一',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='邮件规则时间';
SET FOREIGN_KEY_CHECKS = 1;

ALTER TABLE `shoping`.`orders`
ADD COLUMN `is_run_emailtime` tinyint(2) UNSIGNED NULL DEFAULT 0 COMMENT '是否合成了邮件时间' AFTER `is_meet_email_sending_rule`;


todo
ALTER TABLE `shoping`.`orders_email_time`
MODIFY COLUMN `dm` bigint(20) UNSIGNED NULL DEFAULT 0 COMMENT '所属分' AFTER `pay_time`;


/www/server/php/74/bin/php /www/wwwroot/demo.one/think other etime 2>&1
/www/server/php/74/bin/php /www/wwwroot/demo.one/think other etime_ex 2>&1

 */
/**
 * 处理邮件时间
 */
class etime
{
    /**
     * 运行频率 (这个  决定 发送邮箱指令的 发送频率)
     * @var int
     */
    protected $per_min = 3;

    // 配置规则 todo  按杜乐的
    // 第一次   1 小时     0.5
    // 第二次   24 小时     2
    // 第三次   72 小时     12
    // 第四次   720 小时    24
    protected $rule_config = [
        'one' => 0.5,
        'two' => 2,
        'three' => 12,
        'four' => 24,
    ];

    /**
     * 最大时间跨度
     * @var int
     */
    protected $max_time_span = 720;

    /**
     * 1 小时 多少秒
     * @var int
     */
    protected $hour_time = 3600;

    /**
     * 维护 orders_email_time 表
     * @return void
     */
    public function do()
    {
        try {
            $this->_fill_table();
        } catch (\Exception $e) {
            Log::email(__METHOD__."弃单召回，时间匹配出现问题:".$e->getMessage());
        }
    }

    /**
     * 1 . 表数据回填
     * @return void
     */
    private function _fill_table(){
        Db::transaction(function (){
            ShopingConn::getConn()
                ->table(Table::Table_Order)
                ->field('id,created_at,pay_time')
                ->where('is_run_emailtime',0)
                ->chunk(200,function ($datas){
                    if ($datas){
                        foreach ($datas as $row){
                            $base_time = $row['created_at'];

                            $dm = date('ymdHi',$base_time);
                            $dscale = $this->_get_scale($base_time);
                            $dscale_rule_one = $this->_get_scale($base_time+$this->rule_config['one'] * $this->hour_time);
                            $dscale_rule_two = $this->_get_scale($base_time+$this->rule_config['two'] * $this->hour_time);
                            $dscale_rule_three = $this->_get_scale($base_time+$this->rule_config['three'] * $this->hour_time);
                            $dscale_rule_four = $this->_get_scale($base_time+$this->rule_config['four'] * $this->hour_time);

                            $inRow = compact("dm","dscale","dscale_rule_one","dscale_rule_two","dscale_rule_three","dscale_rule_four");
                            $inRow['order_id'] = $row['id'];
                            $inRow['created_at'] = $base_time;

                            if ($row['pay_time']) $inRow['pay_time'] = $row['pay_time'];
                            $value = ShopingConn::getConn()->table(Table::Table_Order_Email_Time)->where('order_id', $inRow['order_id'])->value('id');
                            if ($value){
                                // 更新
                                ShopingConn::getConn()->table(Table::Table_Order_Email_Time)
                                    ->where('id',$value)
                                    ->update($inRow);
                            }else{
                                // 新增
                                ShopingConn::getConn()->table(Table::Table_Order_Email_Time)
                                    ->insert($inRow);
                            }
                            ShopingConn::getConn()->table(Table::Table_Order)->where('id',$row['id'])->update(['is_run_emailtime'=>1]);
                        }
                    }

                });
        });
    }


    /**
     * 获取刻度时间
     * @param $base_time
     * @return string
     */
    protected function _get_scale($base_time)
    {
        $dh = date('ymdH',$base_time);
        $i = date('i',$base_time);
        return $dh.floor($i/$this->per_min);
    }
}
